/*
 * @lc app=leetcode id=328 lang=cpp
 *
 * [328] Odd Even Linked List
 */
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (!head || !head->next || !head->next->next) {
            return head;
        }

        ListNode *podd = head;
        ListNode *peven = head->next;

        ListNode *odd = podd;
        ListNode *even = peven;
        ListNode *node = even->next;
        while(node) {
            odd->next = node;
            odd = odd->next;
            node = node->next;
            if (node) {
                even->next = node;
                even = even->next;
                node = node->next;
            }
        }
        even->next = NULL;
        odd->next = NULL;
        odd->next = peven;
        return podd;
    }
};

